package com.yichen.ycapiInterface.controller;


import com.yichen.ycapiclientSDK.model.User;
import com.yichen.ycapiclientSDK.utils.SignUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * @Author: yichenwu
 * @Date:2024/7/29 16:04
 */
@RestController
@RequestMapping("/name")
public class NameController {
    @GetMapping("/")
    public String getNameByGet(String name) {
        return "GET 你的名字是" + name;
    }

    @PostMapping("/")
    public String getNameByPost(@RequestParam String name) {
        return "POST 你的名字是" + name;
    }

    @PostMapping("/user")
    public String getUserNameByPost(@RequestBody User user, HttpServletRequest request) {
//        String accessKey = Optional.ofNullable(request.getHeader("accessKey")).orElse(" ");
//        String nonce = Optional.ofNullable(request.getHeader("nonce")).orElse(" ");
//        String timestamp = Optional.ofNullable(request.getHeader("timestamp")).orElse(" ");
//        String sign = Optional.ofNullable(request.getHeader("sign")).orElse(" ");
//        String requestBody = Optional.ofNullable(request.getHeader("requestBody")).orElse(" ");
//        // todo 实际情况应该是去数据库中查是否已分配给用户
//        if (!accessKey.equals("yichen")) {
//            throw new RuntimeException("无权限");
//        }
//        // 校验随机数,模拟一下,直接判断nonce是否大于10000
//        if (Long.parseLong(nonce) > 10000) {
//            throw new RuntimeException("无权限");
//        }
//        // 时间和当前时间不能超过5分钟
//        final long FIVE_MINUTES = 60 * 5;
//        if (Long.parseLong(timestamp) + FIVE_MINUTES > (System.currentTimeMillis() / 1000)) {
//            throw new RuntimeException("无权限");
//        }
//        String serverSign = SignUtils.getSign(requestBody, "yichenwu");
//        if (!serverSign.equals(sign)) {
//            throw new RuntimeException("无权限");
//        }
        return "POST 用户名字是" + user.getUserName();
    }
}
